package cn.mugutu.finance.controller;

import cn.mugutu.finance.annotation.SysLog;
import cn.mugutu.finance.annotation.SysResource;
import cn.mugutu.finance.domain.system.Menu;
import cn.mugutu.finance.domain.system.User;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.dto.EasyUIResult;
import cn.mugutu.finance.dto.MenuDto;
import cn.mugutu.finance.exception.BusinessException;
import cn.mugutu.finance.query.MenuQuery;
import cn.mugutu.finance.service.system.MenuService;
import cn.mugutu.finance.utils.ConstantConfig;
import cn.mugutu.finance.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取当前用户拥有的菜单
     * @return
     */
    @RequestMapping("/getlist")
    @ResponseBody
    public List<Menu> getlist() {
        List<Menu> menus = (List<Menu>) session.getAttribute(ConstantConfig.LOGIN_USER_MENUS);
        if (menus == null || menus.size() == 0) {
            User user = getLoginUser();
            menus = menuService.findAllMenus(user);
            session.setAttribute(ConstantConfig.LOGIN_USER_MENUS, menus);
        }
        return menus;
    }

    @RequestMapping("/listUI")
    @SysResource(name = "菜单管理", code = "menu:listui")
    public String listUI() {
        return "menu/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIResult<MenuDto> list(MenuQuery query, int page, int rows) {
        setEasyUIParamsInQuery(query, page, rows);
        Pager<Menu> pager = menuService.findByPage(query);
        List<MenuDto> dtoList = MenuDto.menuListToMenuDtoList(pager.getRows());
        return new EasyUIResult<MenuDto>(pager.getTotalCount(), dtoList);
    }

    @RequestMapping("/get")
    @ResponseBody
    public Menu getById(Long id) {
        return menuService.get(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public BaseResult update(Menu menu, HttpSession session) {
        return menuService.updateMenu(menu, getLoginUser(), session);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @SysLog(description = "删除菜单")
    public BaseResult delete(Long id) {
        try {
            menuService.delete(id);
            return new BaseResult(true, "删除成功");
        } catch (Exception e) {
            throw new BusinessException("删除失败,该菜单是一级菜单或者该菜单已经赋予角色,请联系系统管理员");
        }
    }

    @RequestMapping("/allSecondMenu")
    @ResponseBody
    public List<MenuDto> findAllOneSecondMenu(Long id) {
        List<Menu> list = menuService.findAllSecondLeveMenu();
        return MenuDto.menuListToMenuDtoList(list);
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<MenuDto> findAll(Long id) {
        List<Menu> list = menuService.findAll();
        return MenuDto.menuListToMenuDtoList(list);
    }

}
